﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using PS.DBUtility;
namespace PS.DAL
{
    /// <summary>
    /// 数据访问类:RepResourceMang
    /// </summary>
    public partial class RepResourceMang
    {
        public RepResourceMang()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录




        /// </summary>
        public bool Exists(int RepResourceMangID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from RepResourceMang");
            strSql.Append(" where RepResourceMangID=@RepResourceMangID");
            SqlParameter[] parameters = {
					new SqlParameter("@RepResourceMangID", SqlDbType.Int,4)
};
            parameters[0].Value = RepResourceMangID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据




        /// </summary>
        public int Add(PS.Model.RepResourceMang model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into RepResourceMang(");
            strSql.Append("ResourceName,ResourceCode,ResourceModel,ResourceType,CurrentStatus,Memo,Spare1,Spare2,Spare3,Spare4,Creator,CreateTime,Editor,EditTime,DispOrder,ValidMark)");
            strSql.Append(" values (");
            strSql.Append("@ResourceName,@ResourceCode,@ResourceModel,@ResourceType,@CurrentStatus,@Memo,@Spare1,@Spare2,@Spare3,@Spare4,@Creator,@CreateTime,@Editor,@EditTime,@DispOrder,@ValidMark)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@ResourceName", SqlDbType.VarChar,100),
					new SqlParameter("@ResourceCode", SqlDbType.VarChar,100),
					new SqlParameter("@ResourceModel", SqlDbType.VarChar,100),
					new SqlParameter("@ResourceType", SqlDbType.Int,4),
					new SqlParameter("@CurrentStatus", SqlDbType.VarChar,20),
					new SqlParameter("@Memo", SqlDbType.VarChar,2000),
					new SqlParameter("@Spare1", SqlDbType.VarChar,200),
					new SqlParameter("@Spare2", SqlDbType.VarChar,200),
					new SqlParameter("@Spare3", SqlDbType.VarChar,200),
					new SqlParameter("@Spare4", SqlDbType.VarChar,200),
					new SqlParameter("@Creator", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Editor", SqlDbType.Int,4),
					new SqlParameter("@EditTime", SqlDbType.DateTime),
					new SqlParameter("@DispOrder", SqlDbType.Int,4),
					new SqlParameter("@ValidMark", SqlDbType.Int,4)};
            parameters[0].Value = model.ResourceName;
            parameters[1].Value = model.ResourceCode;
            parameters[2].Value = model.ResourceModel;
            parameters[3].Value = model.ResourceType;
            parameters[4].Value = model.CurrentStatus;
            parameters[5].Value = model.Memo;
            parameters[6].Value = model.Spare1;
            parameters[7].Value = model.Spare2;
            parameters[8].Value = model.Spare3;
            parameters[9].Value = model.Spare4;
            parameters[10].Value = model.Creator;
            parameters[11].Value = model.CreateTime;
            parameters[12].Value = model.Editor;
            parameters[13].Value = model.EditTime;
            parameters[14].Value = model.DispOrder;
            parameters[15].Value = model.ValidMark;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据




        /// </summary>
        public bool Update(PS.Model.RepResourceMang model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update RepResourceMang set ");
            strSql.Append("ResourceName=@ResourceName,");
            strSql.Append("ResourceCode=@ResourceCode,");
            strSql.Append("ResourceModel=@ResourceModel,");
            strSql.Append("ResourceType=@ResourceType,");
            strSql.Append("CurrentStatus=@CurrentStatus,");
            strSql.Append("Memo=@Memo,");
            strSql.Append("Spare1=@Spare1,");
            strSql.Append("Spare2=@Spare2,");
            strSql.Append("Spare3=@Spare3,");
            strSql.Append("Spare4=@Spare4,");
            strSql.Append("Creator=@Creator,");
            strSql.Append("CreateTime=@CreateTime,");
            strSql.Append("Editor=@Editor,");
            strSql.Append("EditTime=@EditTime,");
            strSql.Append("DispOrder=@DispOrder,");
            strSql.Append("ValidMark=@ValidMark");
            strSql.Append(" where RepResourceMangID=@RepResourceMangID");
            SqlParameter[] parameters = {
					new SqlParameter("@ResourceName", SqlDbType.VarChar,100),
					new SqlParameter("@ResourceCode", SqlDbType.VarChar,100),
					new SqlParameter("@ResourceModel", SqlDbType.VarChar,100),
					new SqlParameter("@ResourceType", SqlDbType.Int,4),
					new SqlParameter("@CurrentStatus", SqlDbType.VarChar,20),
					new SqlParameter("@Memo", SqlDbType.VarChar,2000),
					new SqlParameter("@Spare1", SqlDbType.VarChar,200),
					new SqlParameter("@Spare2", SqlDbType.VarChar,200),
					new SqlParameter("@Spare3", SqlDbType.VarChar,200),
					new SqlParameter("@Spare4", SqlDbType.VarChar,200),
					new SqlParameter("@Creator", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@Editor", SqlDbType.Int,4),
					new SqlParameter("@EditTime", SqlDbType.DateTime),
					new SqlParameter("@DispOrder", SqlDbType.Int,4),
					new SqlParameter("@ValidMark", SqlDbType.Int,4),
					new SqlParameter("@RepResourceMangID", SqlDbType.Int,4)};
            parameters[0].Value = model.ResourceName;
            parameters[1].Value = model.ResourceCode;
            parameters[2].Value = model.ResourceModel;
            parameters[3].Value = model.ResourceType;
            parameters[4].Value = model.CurrentStatus;
            parameters[5].Value = model.Memo;
            parameters[6].Value = model.Spare1;
            parameters[7].Value = model.Spare2;
            parameters[8].Value = model.Spare3;
            parameters[9].Value = model.Spare4;
            parameters[10].Value = model.Creator;
            parameters[11].Value = model.CreateTime;
            parameters[12].Value = model.Editor;
            parameters[13].Value = model.EditTime;
            parameters[14].Value = model.DispOrder;
            parameters[15].Value = model.ValidMark;
            parameters[16].Value = model.RepResourceMangID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据




        /// </summary>
        public bool Delete(int RepResourceMangID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from RepResourceMang ");
            strSql.Append(" where RepResourceMangID=@RepResourceMangID");
            SqlParameter[] parameters = {
					new SqlParameter("@RepResourceMangID", SqlDbType.Int,4)
};
            parameters[0].Value = RepResourceMangID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据




        /// </summary>
        public bool DeleteList(string RepResourceMangIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from RepResourceMang ");
            strSql.Append(" where RepResourceMangID in (" + RepResourceMangIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体




        /// </summary>
        public PS.Model.RepResourceMang GetModel(int RepResourceMangID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 RepResourceMangID,ResourceName,ResourceCode,ResourceModel,ResourceType,CurrentStatus,Memo,Spare1,Spare2,Spare3,Spare4,Creator,CreateTime,Editor,EditTime,DispOrder,ValidMark from RepResourceMang ");
            strSql.Append(" where RepResourceMangID=@RepResourceMangID");
            SqlParameter[] parameters = {
					new SqlParameter("@RepResourceMangID", SqlDbType.Int,4)
};
            parameters[0].Value = RepResourceMangID;

            PS.Model.RepResourceMang model = new PS.Model.RepResourceMang();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["RepResourceMangID"].ToString() != "")
                {
                    model.RepResourceMangID = int.Parse(ds.Tables[0].Rows[0]["RepResourceMangID"].ToString());
                }
                model.ResourceName = ds.Tables[0].Rows[0]["ResourceName"].ToString();
                model.ResourceCode = ds.Tables[0].Rows[0]["ResourceCode"].ToString();
                model.ResourceModel = ds.Tables[0].Rows[0]["ResourceModel"].ToString();
                if (ds.Tables[0].Rows[0]["ResourceType"].ToString() != "")
                {
                    model.ResourceType = int.Parse(ds.Tables[0].Rows[0]["ResourceType"].ToString());
                }
                model.CurrentStatus = ds.Tables[0].Rows[0]["CurrentStatus"].ToString();
                model.Memo = ds.Tables[0].Rows[0]["Memo"].ToString();
                model.Spare1 = ds.Tables[0].Rows[0]["Spare1"].ToString();
                model.Spare2 = ds.Tables[0].Rows[0]["Spare2"].ToString();
                model.Spare3 = ds.Tables[0].Rows[0]["Spare3"].ToString();
                model.Spare4 = ds.Tables[0].Rows[0]["Spare4"].ToString();
                if (ds.Tables[0].Rows[0]["Creator"].ToString() != "")
                {
                    model.Creator = int.Parse(ds.Tables[0].Rows[0]["Creator"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Editor"].ToString() != "")
                {
                    model.Editor = int.Parse(ds.Tables[0].Rows[0]["Editor"].ToString());
                }
                if (ds.Tables[0].Rows[0]["EditTime"].ToString() != "")
                {
                    model.EditTime = DateTime.Parse(ds.Tables[0].Rows[0]["EditTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DispOrder"].ToString() != "")
                {
                    model.DispOrder = int.Parse(ds.Tables[0].Rows[0]["DispOrder"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ValidMark"].ToString() != "")
                {
                    model.ValidMark = int.Parse(ds.Tables[0].Rows[0]["ValidMark"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RepResourceMangID,ResourceName,ResourceCode,ResourceModel,ResourceType,CurrentStatus,Memo,Spare1,Spare2,Spare3,Spare4,Creator,CreateTime,Editor,EditTime,DispOrder,ValidMark ");
            strSql.Append(" FROM RepResourceMang ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetTypeList(out long totalcount)
        {
            string sql = "SELECT [ID],[MainKeyID],[SubKeyName],[KeyValue],[Creator],[CreateTime],[Editor],[EditTime],[DispOrder],[ValidMark] FROM [SysDictionarySub] where [MainKeyID]=14";
            totalcount = long.Parse(DbHelperSQL.ExecuteScalar(sql).ToString());
            return DbHelperSQL.Query(sql);
        }

        /// <summary>
        /// 获得前几行数据




        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" RepResourceMangID,ResourceName,ResourceCode,ResourceModel,ResourceType,CurrentStatus,Memo,Spare1,Spare2,Spare3,Spare4,Creator,CreateTime,Editor,EditTime,DispOrder,ValidMark ");
            strSql.Append(" FROM RepResourceMang ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataSet GetList(string resourceName,string resourceCode,string ResourceModel,int resourceType,string currentStatus,int pageSize,int pageIndex,out long totalCount)
        {
             StringBuilder strSql = new StringBuilder();
             strSql.Append("select a.*,b.keyvalue as resourcetypename ,(Case  a.CurrentStatus when '修理' then 'blue' when '在用' then 'red' else 'black' end ) as FontColor from RepResourceMang a left join dbo.SysDictionarySub b on a.resourcetype=b.id and b.mainkeyid=14 where a.ValidMark=1 ");
            if (!string.IsNullOrEmpty(resourceName))
            {
                strSql.Append(" and a.resourceName like '%").Append(resourceName).Append("%'");
            }
            if (!string.IsNullOrEmpty(resourceCode))
            {
                strSql.Append(" and a.resourceCode  like '%").Append(resourceCode + "%'");
            }
            if (!string.IsNullOrEmpty(ResourceModel))
            {
                strSql.Append(" and a.ResourceModel  like '%").Append(ResourceModel + "%'");
            }
            if (resourceType.ToString() != "0")
            {
                strSql.Append(" and a.resourceType  = ").Append(resourceType);
            }
            if (!string.IsNullOrEmpty(currentStatus))
            {
                strSql.Append(" and a.CurrentStatus  = '").Append(currentStatus + "'");
            }


            strSql.Append("  order by a.RepResourceMangID");


            string totalSql = PageSplitHelper.GetTotalSql(strSql.ToString());
            totalCount = long.Parse(DbHelperSQL.ExecuteScalar(totalSql).ToString());

            string pageSql = PageSplitHelper.GetPageSql(strSql.ToString(), pageSize, pageIndex, totalCount);

            return DbHelperSQL.Query(pageSql);

        }

        public DataSet GetDataByID(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select a.*,b.keyvalue as resourcetypename ,(Case  a.CurrentStatus when '修理' then 'blue' when '在用' then 'red' else 'black' end ) as FontColor from RepResourceMang a left join dbo.SysDictionarySub b on a.resourcetype=b.id and b.mainkeyid=14 where a.ValidMark=1 ");
            strSql.Append(" and a.RepResourceMangID='" + id.ToString()+"'");
            return DbHelperSQL.Query(strSql.ToString());

        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "RepResourceMang";
            parameters[1].Value = "RepResourceMangID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

